<?php namespace App\Http\Controllers\admin\page;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Admin;
use Input;
use App\Models\Page;
use Session;

class PageController extends Controller {

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index()
	{
		//
		return view("admin.page.index")->withIsPage(true);
	}

	public function editing(){

		return view("admin.page.edit_page")->withIsPage(true);
	}

	public function add(){

		return view("admin.page.add_page")->withIsPage(true);
	}

    public function addPage()
    {
	// 	$uid = Session::get("admin")["id"];
	// 	if(is_null($uid)){
	// 		return;
	// 	}else{
	// 	$data = Input::all();
	// 	return $data;
	// 	$data["uid"] = $uid;
	// 	Page::create($data);
	// 	$data = Page::where("uid", $uid)->get()->toArray();
	// 	return $this->json(["data" => $data]);
	// }
 //   }
    	if(!Session::has("admin")){
    		return self::error(self::NOT_LOGIN, '', action('admin\AdminController@login'));
    	}
    	$uid = Session::get("admin")["id"];
    	$pageName = Input::get("page_name");
    	$uniqueKey = Input::get("unique_key");
    	if(!$pageName || $pageName == '' || !$uniqueKey || $uniqueKey == ""){
    		return self::error(self::PARAM_IS_WRONG);
    	}
    	$page = new Page;
    	$page->page_name = $pageName;
    	$page->uid = $uid;
    	$page->unique_key = $uniqueKey;
    	$bool = $page->save();
    	if($bool){
    		return $this->json([
    			"data" => $page
    		]);
    	}
    	return self::error(self::SYSTEM_ERROR);
    }


    public function getPage()
    {
    	$pid = Input::get("pid");
    	$model = Page::where("id",$pid);
    	if($model->count()){
    		$data = $model->get()[0]->toArray();
	    	return $this->json(["data" => $data]);
	    }
    	return $this->json([]);
    }

    public function getDetilPage(){
    	$pid = Input::get("pid");
    	$model= Page::where("id", $pid);
    	if($model->count()){
    		$data = $model->first()->toArray();
    		return $this->json(["data" => $data]);
    	}
    	return $this->json([]);
    }

    public function updatePage()
    {
    	if(!Session::has("admin")){
    		return self::error(self::NOT_LOGIN, '', action('admin\AdminController@login'));
    	}
    	$pid = Input::get("pid");
    	$uid = Session::get("admin")["id"];
    	$pageName = Input::get("page_name");
    	if(!$pageName || $pageName == ''){
    		return self::error(self::PARAM_IS_WRONG);
    	}

    	$model = Page::where("id", $pid);
    	if($model->count()){
            $data = $model->update([
            	"uid" => $uid,
            	"page_name" => $pageName,
            	"page_content" => Input::get("page_content")
            ]);
            return $this->json([]);
    	}
    	return self::error(self::NO_DATA);
    }

    public function pageList()
	{
		$current = (int)Input::get("current");
		$rowCount = (int)Input::get("rowCount");
		$keyword = Input::get("searchPhrase");
		$sort = Input::get("sort")["id"];
		$sort = $sort ? $sort : "desc";
        $model = Page::with("admin")->orderBy("id", $sort);
		$count = $model->get()->count();
		$data = $model->skip(($current - 1) * $rowCount)->take($rowCount)->where("page_name", "like", "%" . $keyword . "%")->get();
		return array(
			"current" => $current,
			"rowCount" => $rowCount,
			"rows" => $data->toArray(),
			"total" => $count
		);
	}

	public function deletePage(){
		$pid = Input::get("pid");
		$model = Page::where("id", $pid);
		if($model->count()){
			$model->delete();
			return $this->json([]);
		}
		return self::error(self::NO_DATA);
	}
	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create()
	{
		//
	}

	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		//
	}

	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		//
	}

	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		//
	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		//
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		//
	}

}
